<?php

namespace App\Models;

use App\Common\Constant\Params;
use App\Services\IStorageService;
use Illuminate\Support\Collection;

class OrderComplain extends BaseModel
{

    protected $table = 'order_complain';

    protected $primaryKey = 'oc_id';

    const CREATED_AT = 'oc_add_time';
    const UPDATED_AT = null;

    protected $dateFormat = 'U';

    protected $fillable = [
        'oc_orderid',
        'oc_meid',
        'oc_category',
        'oc_subcategory',
        'oc_content',
        'oc_img',
        'oc_number',
        'oc_handle_status',
        'oc_handle_result',
        'oc_handle_time',
        'oc_handle_explain',
        'oc_punish_amount',
    ];

    protected $casts = [
        'oc_img' => 'array',
        'oc_handle_time' => 'datetime',
        'oc_punish_amount'=>'double',
    ];

    protected $touches = ['order'];

    public function order(){
        return $this->belongsTo(Order::class,'oc_orderid');
    }

    public function merchant(){
        return $this->belongsTo(Merchant::class,'oc_meid');
    }

    /**
     * 是否正在等待处理
     * complaining
     */
    public function getComplainingAttribute()
    {
        return $this->oc_handle_status == Params::COMPLAIN_STATUS_WAITING;
    }

    /**
     * 获取投诉类别文本
     * category_text
     */
    public function getCategoryTextAttribute()
    {
        return Params::$COMPLAIN_CATEGORY[$this->oc_category];
    }

    /**
     * 获取投诉子分类文本
     * subcategory_text
     */
    public function getSubcategoryTextAttribute()
    {
        return Params::$COMPLAIN_SUBCATEGORY[$this->oc_subcategory];
    }

    /**
     * 获取处理状态文本
     * status_text
     */
    public function getStatusTextAttribute()
    {
        return Params::$COMPLAIN_STATUS[$this->oc_handle_status];
    }

    /**
     * 获取处理结果文本
     * result_text
     */
    public function getResultTextAttribute()
    {
        return Params::$COMPLAIN_RESULT[$this->oc_handle_result];
    }

    /**
     * 投诉证据图片url
     * complain_imgs_url
     */
    public function getComplainImgsUrlAttribute()
    {
        return Collection::make($this->oc_img)->transform(function($item,$key){
            return resolve(IStorageService::class)->getUrl($item);
        });
    }
}
